from itsdangerous import BadData
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
# SECRET_KEY = '6c^=xdi6eli&2!&*m(!m!s!*dt(kjtd-ec7@@6fdi@v5bx=(sp'
#
# # serializer = Serializer(秘钥, 有效期秒)
# serializer = Serializer(SECRET_KEY, 300)
# # serializer.dumps(数据), 返回bytes类型
# d={'openid': '18512345678'}
# token = serializer.dumps(d)
# token = token.decode()
#
# # 检验token
# # 验证失败，会抛出itsdangerous.BadData异常
# serializer = Serializer(settings.SECRET_KEY, 300)
# try:
#     data = serializer.loads(token)
# except BadData:
#     print('bad_data')

from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

def encode_signature_openid(openid):
    serialzer=Serializer(settings.SECRET_KEY,300)
    token=serialzer.dumps({'openid':openid})
    return token.decode()

def decode_signature_openid(token):
    serialize=Serializer(settings.SECRET_KEY,300)
    try:
        data=serialize.loads(token)
    except Exception as e:
        return None
    return data['openid']













